// pages/applyStore/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        shop: {
            phone: '',
            shopname: "",
            address: "",
            province_code: "",
            city_code: "",
            area_code: "",
            street_code: '',
            imagesurl: "",
            idcardimg1: "",
            idcardimg2: "",
            licenseimg: "",
            shop_manager: "",
            lon: "",
            lat: "",
            user_pass: "",
            apply_type: "",
            remark: "",
            // salecode: "",
        },
        location: '',
        idcardimg1: [],
        dcardimg2: [],
        imagesurl: [],
        licenseimg: [],
        arealist: [],
        address: "",
        apply_type: "",
        showTypeChoose: false,
        showCityChoose: false,
        typeList: ['商城（线上）', '家政服务'],
        // 商家街道
        shopStreetText: '请选择'
    },
    // 四级联动异步加载
    onCascaderChange(e) {
        console.log(e);
        let {
            code,
            value
        } = e.detail
        this.data.shop.province_code = code[0] || ''
        this.data.shop.city_code = code[1] || ''
        this.data.shop.area_code = code[2] || ''
        this.data.shop.street_code = code[3] || ''
        this.setData({
            shopStreetText: value.join(' ')
        })
        console.log(this.data.shop);
    },
    //上传创建店铺数据
    applyStore() {
        let {
            shop
        } = this.data
        if (!shop.street_code) {
            return wx.showToast({
                title: '请选择街道',
                icon: 'none'
            })
        }
        wx.http.post(wx.api.register, this.data.shop).then((res) => {
            if (res.code == 1) {
                wx.showToast({
                    title: res.msg,
                    icon: 'success',
                    duration: 1000
                })
                let timer = setTimeout(() => {
                    wx.switchTab({
                        url: '/pages/my/index'
                    })
                    clearTimeout(timer)
                }, 1000);

            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'none',
                    duration: 1000
                })
            }
        })


    },
    //用户输入数据
    onChange(event) {

        switch (event.target.dataset.name) {
            case "shopname":
                this.setData({
                    'shop.shopname': event.detail
                })

                break;
            case "remark":
                this.setData({
                    'shop.remark': event.detail
                })
                break;
            case "shop_manager":
                this.setData({
                    'shop.shop_manager': event.detail
                })
                break;
            case "user_pass":
                this.setData({
                    'shop.user_pass': event.detail
                })
                break;
            case "address":
                this.setData({
                    'shop.address': event.detail
                })
                break;
            case "salecode":
                this.setData({
                    'shop.salecode': event.detail
                })
                break;
            case "salecode":
                this.setData({
                    'shop.salecode': event.detail
                })
                break;
            case "phone":
                this.setData({
                    'shop.phone': event.detail
                })
                break;
        }
    },
    //上传文件
    uploadtheimage(event) {
        const {
            file
        } = event.detail;
        console.log(file);
        if (file.size >= 5242880) {
            return wx.showToast({
                title: '图片不能超过5M',
                icon: "none"
            })
        }
        var that = this;
        wx.uploadFile({
            url: `${wx.api.host}/h5/uploadshopimage`,
            filePath: file.url,
            name: 'image',
            formData: {
                type_name: 'applyreg'
            },
            header: {
                token: wx.getStorageSync('token')
            },
            success(res) {
                // 上传完成需要更新 fileList
                if (res.statusCode != 200) {
                    wx.showToast({
                        title: '上传失败',
                        icon: 'none'
                    })
                }
                let data = JSON.parse(res.data)
                let url = [{
                    url: `${wx.api.host}/${data.data.file_path}`
                }]
                switch (event.target.dataset.image) {
                    case "idcardimg1":
                        that.setData({
                            idcardimg1: url,
                            'shop.idcardimg1': data.data.file_path
                        })

                        break;
                    case "idcardimg2":
                        that.setData({
                            idcardimg2: url,
                            'shop.idcardimg2': data.data.file_path
                        })
                        break;
                    case "imagesurl":
                        that.setData({
                            imagesurl: url,
                            'shop.imagesurl': data.data.file_path
                        })
                        break;
                    case "licenseimg":
                        that.setData({
                            licenseimg: url,
                            'shop.licenseimg': data.data.file_path
                        })
                        break;

                }
            },
            fail: err => {
                wx.showToast({
                    title: '上传失败',
                    icon: 'none'
                })
                console.error(err);
            }
        });

    },
    // 删除上传图片
    deleteImage(event) {

        switch (event.target.dataset.image) {
            case "idcardimg1":
                this.setData({
                    idcardimg1: [],
                    'shop.idcardimg1': ''
                })
                break;
            case "idcardimg2":
                this.setData({
                    idcardimg2: [],
                    'shop.idcardimg2': ''
                })
                break;
            case "imagesurl":
                this.setData({
                    imagesurl: [],
                    'shop.imagesurl': ''
                })
                break;
            case "licenseimg":
                this.setData({
                    licenseimg: [],
                    'shop.licenseimg': ''
                })
                break;

        }

    },
    // 选择申请店铺类型
    showType() {

        this.setData({
            showTypeChoose: "true"
        })
    },
    showCityChoose() {

        this.setData({
            showCityChoose: "true"
        })
    },
    //取消选择店铺类型
    onCancelType() {

        this.setData({

            showTypeChoose: false,

        })
    },
    onCancelCity() {
        this.setData({

            showCityChoose: false,
        })
    },
    //选择店铺类型
    onConfirmType(e) {

        this.setData({
            apply_type: e.detail.value,
            showTypeChoose: false,
            'shop.apply_type': e.detail.index + 1
        })

    },
    chooseAddress() {
        let that = this
        wx.chooseLocation({
            success: res => {
                that.setData({
                    'shop.lon': res.longitude,
                    'shop.lat': res.latitude,
                    location: res.address
                })
            },
            fail: err => {
                console.error(err);
            }
        })
    },
    onConfirmCity(e) {
        let list = e.detail.values
        this.setData({
            address: list[0].name + list[1].name + list[2].name,
            'shop.province_code': list[0].code,
            'shop.city_code': list[1].code,
            'shop.area_code': list[2].code,
            showCityChoose: false,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        var that = this
        const {
            lon,
            lat
        } = wx.getStorageSync('latlon')
        if (lon) {
            that.setData({
                'shop.lon': lon,
                'shop.lat': lat
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})